﻿using System;
using System.Transactions;
using Bootstrap;
using Bootstrap.AutoMapper;
using Dogshow.BusinessLogic.Utils;
using Dogshow.Model;
using DogshowRepository;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace Dogshow.Linq2SQLRepository.Test
{
    [TestClass]
    public class PsiRepositoryFixtures
    {
        private IPsiRepository m_Cut;

        [ClassInitialize]
        public static void ClassInit(TestContext ctx)
        {
            var model = new DogshowModel();
            if (model.DatabaseExists())
            {
                model.DeleteDatabase();
            }
            model.CreateDatabase();
            model.ExecuteCommand("INSERT INTO Mesta VALUES ('21000', 'NOVI SAD')");
            model.ExecuteCommand("INSERT INTO Mesta VALUES ('11000', 'BEOGRAD')");
            model.ExecuteCommand("INSERT INTO FCIGrupe VALUES ('III FCI Grupa', 'Molosi')");
            model.ExecuteCommand("INSERT INTO Kinolozi VALUES (0)");
            model.ExecuteCommand("INSERT INTO Kinolozi VALUES (0)");
            model.ExecuteCommand("INSERT INTO RasePasa VALUES ('Bullmastiff', 1, 'BULLMASTIFF')");
            model.ExecuteCommand("INSERT INTO Psi VALUES ('BLACK DIAMOND', 'JR/89789', NULL, '2006-8-3', 1, NULL, NULL, NULL, NULL, 'crvena', 'kratka', 78, 94, 1)");
            model.ExecuteCommand("INSERT INTO Psi VALUES ('AGATA', 'JR/43889', NULL, '2006-1-9', 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 2)");
            model.ExecuteCommand("INSERT INTO Psi VALUES ('CELTIC', 'JR/76576', NULL, '2006-1-9', 1, 1, 2, 1, 2, NULL, NULL, NULL, NULL, 1)");
            model.ExecuteCommand("INSERT INTO Titule VALUES ('C.A.C')");
            model.ExecuteCommand("INSERT INTO Titule VALUES ('C.A.C.I.B')");
            model.ExecuteCommand("INSERT INTO TitulePasa VALUES(1, 1, 4)");
            model.ExecuteCommand("INSERT INTO TitulePasa VALUES(2, 1, 3)");
            var lozinka = new Md5Encryptor().Encrypt("1302");
            //model.ExecuteCommand(string.Format("INSERT INTO Korisnici VALUES ('timit', 'Milan Timotić', CONVERT(VARBINARY(MAX), '{0}'), 1, NULL, 1, 'rottor@sbb.rs')", lozinka));
            model.ExecuteCommand(string.Format("INSERT INTO Korisnici VALUES ('zeks', 'Željko Bajšanski', CONVERT(VARBINARY(MAX), '{0}'), 1, '063528065', 'Preradoviceva 36', 1, 'zeljko.bajsanski@gmail.com', NULL, 1, 1)", lozinka));
            model.ExecuteCommand("INSERT INTO OmiljeniPsi VALUES ('zeks', 1)");
            model.ExecuteCommand("INSERT INTO OmiljeniPsi VALUES ('zeks', 2)");

        }

        [TestInitialize]
        public void TestInit()
        {
            Bootstrapper.With.AutoMapper().Start();
            m_Cut = new PsiRepository();
            
        }

        [TestMethod]
        public void Insert()
        {
            using (new TransactionScope())
            {
                // Arrange
                var pas = new Model.Pas
                {
                    ImePsa = "Blue Chelsea",
                    BrojRodovnika = "JR/921379",
                    Ostenjen = new DateTime(2007, 11, 27),
                    IdRase = 1,
                    IdVlasnika = 1,
                    IdOca = 1,
                    IdMajke = 2,
                    IdOdgajivaca = 1,
                    BojaDlake = "Crvena",
                    TipDlake = "Kratka",
                    Visina = 56,
                    Tezina = 56
                };

                // Act
                var id = m_Cut.Sacuvaj(pas);

                // Assert
                Assert.IsTrue(id > 0);
            }
        }

        [TestMethod]
        public void Update()
        {
            using (new TransactionScope())
            {
                var pas = m_Cut.VratiPoId(3);

                pas.ImePsa = "Sotonajlo";
                pas.BrojRodovnika = "JR/9892788";
                pas.IdOca = 2;
                pas.IdMajke = 1;
                pas.Ostenjen = DateTime.Now;
                pas.IdVlasnika = 2;
                pas.IdOdgajivaca = 1;

                // Act
                m_Cut.Sacuvaj(pas);

                // Assert
                var pas1 = m_Cut.VratiPoId(3);
                Assert.AreEqual(pas.ImePsa, pas1.ImePsa);
                Assert.AreEqual(pas.BrojRodovnika, pas1.BrojRodovnika);
                Assert.AreEqual(pas.IdOca, pas1.IdOca);
                Assert.AreEqual(pas.IdMajke, pas1.IdMajke);
                Assert.AreEqual(pas.IdVlasnika, pas1.IdVlasnika);
                Assert.AreEqual(pas.IdOdgajivaca, pas1.IdOdgajivaca);

            }
        }

        [TestMethod]
        public void Vrati_Po_Id()
        {
            // Arrange

            // Act
            var pas = m_Cut.VratiPoId(1);

            // Assert
            Assert.AreEqual("BLACK DIAMOND", pas.ImePsa);
            Assert.AreEqual("JR/89789", pas.BrojRodovnika);
            Assert.AreEqual(new DateTime(2006, 8,3).Date, pas.Ostenjen);
        }

        [TestMethod]
        public void Dodaj_Titulu()
        {
            Assert.Inconclusive();
        }

        [TestMethod]
        public void Vrati_Info_O_Psima()
        {
            var psi = m_Cut.VratiInfoOPsima();
            Assert.AreEqual(3, psi.Count());
            var prvi = psi.ElementAt(0);
            Assert.AreEqual("III FCI Grupa", prvi.FciGrupa.Naziv);
            Assert.AreEqual("Bullmastiff", prvi.Rasa);
        }

        [TestMethod]
        public void Postoji_Pas_Sa_Rodovnikom()
        {
            // Arrange
            m_Cut = new PsiRepository();

            // Act
            var postojiPas = m_Cut.PostojiPas("JR/43889", 1);

            // Assert
            Assert.IsTrue(postojiPas);
        }

        [TestMethod]
        public void Vrati_Psa_Sa_Rodovnikom()
        {
            // Arrange

            // Act
            var pas = m_Cut.VratiPsaPoBrojuRodovnika("JR/43889");

            // Assert
            Assert.IsNotNull(pas);
            Assert.AreEqual("AGATA", pas.ImePsa);
        }

        [TestMethod]
        public void Vrati_Omiljene_Pse_Korisnika()
        {
            // Act
            var psi = m_Cut.VratiOmiljenePseKorisnika("zeks").ToArray();

            // Assert
            Assert.AreEqual(2, psi.Length);
            Assert.AreEqual("BLACK DIAMOND", psi[0].ImePsa);
            Assert.AreEqual("AGATA", psi[1].ImePsa);
        }

        [TestMethod]
        public void Sacuvaj_Omiljenog_Psa()
        {
            using (new TransactionScope())
            {
                // Arrange
                var pas = new Pas
                {
                    IdRase = 1,
                    BrojRodovnika = "JR/42992",
                    ImePsa = "BlueChelsea",
                    Pol = 1,
                };

                //Act
                m_Cut.SacuvajPsaUOmiljene(pas, "zeks");

                // Assert
                var psiKorisnika = m_Cut.VratiOmiljenePseKorisnika("zeks");
                Assert.IsTrue(psiKorisnika.Any(x => x.ImePsa == "BLUECHELSEA")); 
            }
        }
    }
}